<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<pre>
    <?php
    $filename = 'http://danviet.vn/Rss/25/kinh-te.rss-';
    $filename = 'http://vnexpress.net/rss/tam-su.rss--';
//    $xml = simplexml_load_file($filename);
//    var_dump($xml);
//    foreach ($xml->channel->item as $item) {
//        echo getImageFromDescription($item->description);
//    }
    loadRSS($filename);
    /*  Description: Load conten from rss then insert theme in to DB    */
    /*  Author: MrQuyet */
    /*  Paramater:  */
    /*      String $rss:  link of rss */
    /*  Return: void   */

    function loadRSS($rss) {
        $headers = @get_headers($rss);
        if (strpos($headers[0], '200') === false) {
            return false;
        } else {
            $objRss = @simplexml_load_file($rss);
            if ($objRss) {
                foreach ($objRss->channel->item as $item) {
                    $postData = array(
                        'post_title' => wp_strip_all_tags($item->title),
                        'post_content' => strip_tags($item->description),
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $postID = wp_insert_post($postData);
                    if($postID){
                        
                    }
                    echo getImageFromDescription($item->description);
                }
            }
        }
    }

    /*  Description: Get image url from description of RSS   */
    /*  Author: MrQuyet */
    /*  Paramater:  */
    /*      String $description: Content for need url   */
    /*  Return: string, fasle   */

    function getImageFromDescription($description) {
        if (!empty($description)) {
            $pattern = '/<img[^>]*>/i';
            preg_match($pattern, $description, $imgObj);
            if (count($imgObj)) {
                $pattern = '/http[^\'"]*/i';
                preg_match($pattern, $imgObj[0], $imgUrl);
                return $imgUrl[0];
            }
        }
        return FALSE;
    }
    ?>
</pre>
